package myutil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import common_var.Path;

@SuppressWarnings("unused")
public class RetDoc extends MyDocModel {

	public RetDoc() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public ArrayList<MyDocModel> ImportDocs(String inpath) {
		// TODO Auto-generated method stub
		ArrayList<MyDocModel> list = new ArrayList<MyDocModel>();
		try {
			File docs = new File(inpath);
			if (docs.isFile()) {
				System.err.println("please select the directory");
				return null;
			} else if (docs.isDirectory()) {

				for (File doc : docs.listFiles()) {
					if (doc.getName().endsWith(".formated_all")) {
						BufferedReader bf = new BufferedReader(new FileReader(
								doc));
						try {
							String s = "";
							while ((s = bf.readLine()) != null) {
								RetDoc adoc = new RetDoc();
								String idline = s;
								String[] idpair = idline.split("==");
								String idstring = idpair[0].trim();
								String realid = idpair[1].trim();
								if (idstring.equals("docid"))
									adoc.docid = Integer
											.parseInt(realid);
								while ((s = bf.readLine()).replace(" ", "")
										.length() != 0) {
									try {
										
										String[] porp = s.split("==");
										String k = porp[0].trim();
										String v = porp[1].trim();
										/////////////////////////////////////////////////
										
										
										
										if (porp.length < 2)
											continue;
										if (k.equals("date"))
											adoc.otherfeildnew.put(k, v);
										else if (k.equals("dateline"))
											adoc.otherfeildnew.put(k, v);
										else if (k.equals("places"))
											adoc.otherfeildnew.put(k, v);
										else if (k.equals("people"))
											adoc.otherfeildnew.put(k, v);
										else if (k.equals("orgs"))
											adoc.otherfeildnew.put(k, v);
										else if (k.equals("exchanges"))
											adoc.otherfeildnew.put(k, v);
										else if (k.equals("companies"))
											adoc.otherfeildnew.put(k, v);
										else if (k.equals("topics"))
											adoc.otherfeildnew.put(k, v);
									
										else if (k.equals("title"))
											adoc.title = porp[1];
										else if (k.equals("body"))
											adoc.content = porp[1];

									} catch (Exception e) {
										e.printStackTrace();
									}

								}
								list.add(adoc);
							}
						} catch (Exception e) {
							e.printStackTrace();
						} finally {
							bf.close();
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Import Complete with Total Documents count =" + list.size());
		return list;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		RetDoc rd = new RetDoc();
		ArrayList<MyDocModel> al = rd.ImportDocs(Path.Ret_Data_Locate);
		for (MyDocModel md : al) {
			md.ShowDocsInfo();
			System.out.println("----------------------");
		}
		System.out.println(al.size());
	}

}
